package com.mo.json;

import cn.hutool.core.util.NumberUtil;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONType;
import com.alibaba.fastjson.serializer.ValueFilter;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.mo.entity.Student;

import java.io.IOException;

/**
 * @author moon.pan
 * @program mo-all
 * @apiNote
 * @since 2022/3/28
 */
@JSONType
public class StudentSerializer extends JsonSerializer<Student> {
    @Override
    public void serialize(Student student, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {

        String jsonString = JSONObject.toJSONString(student, (ValueFilter) (object, name, value) -> {
            if (value instanceof Double){
                return NumberUtil.round((Double) value, 2);
            }
            return value;
        });
        jsonGenerator.writeRaw(jsonString);
    }
}
